<link href="<?php echo Yii::app()->theme->baseUrl ?>/css/jquery-ui.css" type="text/css" rel="stylesheet">
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="<?php echo Yii::app()->theme->baseUrl ?>/js/bootstrap-tokenfield/bootstrap-tokenfield.js"></script>
  <link href="<?php echo Yii::app()->theme->baseUrl.'/'; ?>js/bootstrap.min.css" rel="stylesheet">
   <link href="<?php echo Yii::app()->theme->baseUrl ?>/js/bootstrap-tokenfield/css/tokenfield-typeahead.css" type="text/css" rel="stylesheet">
    <!-- Tokenfield CSS -->
    <link href="<?php echo Yii::app()->theme->baseUrl ?>/js/bootstrap-tokenfield/css/bootstrap-tokenfield.css" type="text/css" rel="stylesheet">
    <div class="form-group" id="td_category">
		<?php echo $this->form->labelEx($this->model,'id_loai_hinh_kinhdoanh'); ?>
		<input class="form-control autocomple" placeholder="Loại hình kinh doanh" id="loai_hinh_kinhdoanh" type="text">
		<input type = "hidden" class="text_loaihinh" name="text_loaihinh" value="">
		<?php echo $this->form->hiddenField($this->model,'id_loai_hinh_kinhdoanh',array('class'=>'form-control id_loai_hinh')); ?>
		<?php echo $this->form->error($this->model,'id_loai_hinh_kinhdoanh'); ?>
	</div>

<script type="text/javascript">
		var count = 0 ;
		var arr = {};
		var checkMany = 0;
		function getCategory () {
			var i = 0;
			var str = '';
			if ($('#td_category .token').length == 0) {
				return '("0")';
			};
			$.each($('#td_category .token'), function(index, val) {
				val = $(this);
				if (index == 0) {
					str += '('+val.attr('data-id')+',';
				}else{
					str += ''+val.attr('data-id')+',';
				}
				if (index == $('#td_category .token').length - 1){
					str += val.attr('data-id')+')';
				}
				i++;
			});
			return str;
		}
		$('#loai_hinh_kinhdoanh').on('tokenfield:createtoken', function (e) {
			if (checkMany == 1) {
				alert('Bạn chỉ lựa chọn một loại hình kinh doanh');
				e.preventDefault();
				return false;
			};

			checkMany = 1;
		  }).on('tokenfield:removetoken', function (e) {
			checkMany = 0;
		  }).tokenfield({
				autocomplete: {
			     source: function(request, response) {
			    	$.ajax({
			    		url: '<?php echo Yii::app()->createUrl("/DoiTac/loaihinhkinhdoanh/load"); ?>',
			    		type: 'POST',
			    		dataType: 'json',
			    		data: {p:$('#loai_hinh_kinhdoanh-tokenfield').val(),str:getCategory()},
			    		success: function(data) {
			              	response($.map(data,function (items) {
			              		return{
			              			value: items.name,
			              			id :items.id,
			              		}
			              	}));
			    		}
			    	});
			    },
			    select: function( event, ui ) {
			    	count = 1;
			    	
			    	$('#loai_hinh_kinhdoanh-tokenfield').val('');
					console.log(ui);
					console.log($('#td_category .token').length);
					
					return false;
				},
			    delay: 100
			  },
			  showAutocompleteOnFocus: true
		})
		$('#loai_hinh_kinhdoanh').tokenfield('createToken', <?php echo $value; ?>);
		$( "form" ).submit(function( event ) {
		  	$.each($('#td_category .token'), function(index, val) {
				val = $(this);
				console.log(val);
				$('#td_category .id_loai_hinh').val(val.attr('data-id'));
				$('#td_category .text_loaihinh').val(val.attr('data-value'));
			});
		});
		</script>